using System;
using System.Globalization;
using System.Numerics;
using ns20;

namespace Newtonsoft.Json.Linq
{
	public class JTokenWriter : JsonWriter
	{
		private JContainer jcontainer_0;

		private JContainer jcontainer_1;

		private JValue jvalue_0;

		public JToken Token
		{
			get
			{
				if (jcontainer_0 != null)
				{
					return jcontainer_0;
				}
				return jvalue_0;
			}
		}

		public JTokenWriter(JContainer container)
		{
			Class204.smethod_2(container, "container");
			jcontainer_0 = container;
			jcontainer_1 = container;
		}

		public JTokenWriter()
		{
		}

		public override void Flush()
		{
		}

		public override void Close()
		{
			base.Close();
		}

		public override void WriteStartObject()
		{
			base.WriteStartObject();
			method_20(new JObject());
		}

		private void method_20(JContainer jcontainer_2)
		{
			if (jcontainer_1 == null)
			{
				jcontainer_0 = jcontainer_2;
			}
			else
			{
				jcontainer_1.method_7(jcontainer_2);
			}
			jcontainer_1 = jcontainer_2;
		}

		private void method_21()
		{
			jcontainer_1 = jcontainer_1.Parent;
			if (jcontainer_1 != null && jcontainer_1.Type == JTokenType.Property)
			{
				jcontainer_1 = jcontainer_1.Parent;
			}
		}

		public override void WriteStartArray()
		{
			base.WriteStartArray();
			method_20(new JArray());
		}

		public override void WriteStartConstructor(string name)
		{
			base.WriteStartConstructor(name);
			method_20(new JConstructor(name));
		}

		protected override void WriteEnd(JsonToken token)
		{
			method_21();
		}

		public override void WritePropertyName(string name)
		{
			base.WritePropertyName(name);
			method_20(new JProperty(name));
		}

		private void method_22(object object_0, JsonToken jsonToken_0)
		{
			method_23(new JValue(object_0), jsonToken_0);
		}

		internal void method_23(JValue jvalue_1, JsonToken jsonToken_0)
		{
			if (jcontainer_1 != null)
			{
				jcontainer_1.Add(jvalue_1);
				if (jcontainer_1.Type == JTokenType.Property)
				{
					jcontainer_1 = jcontainer_1.Parent;
				}
			}
			else
			{
				jvalue_0 = jvalue_1 ?? new JValue((object)null);
			}
		}

		public override void WriteValue(object value)
		{
			if (value is BigInteger)
			{
				method_17(JsonToken.Integer);
				method_22(value, JsonToken.Integer);
			}
			else
			{
				base.WriteValue(value);
			}
		}

		public override void WriteNull()
		{
			base.WriteNull();
			method_23(null, JsonToken.Null);
		}

		public override void WriteUndefined()
		{
			base.WriteUndefined();
			method_23(null, JsonToken.Undefined);
		}

		public override void WriteRaw(string json)
		{
			base.WriteRaw(json);
			method_23(new JRaw(json), JsonToken.Raw);
		}

		public override void WriteComment(string text)
		{
			base.WriteComment(text);
			method_23(JValue.CreateComment(text), JsonToken.Comment);
		}

		public override void WriteValue(string value)
		{
			base.WriteValue(value);
			method_22(value ?? string.Empty, JsonToken.String);
		}

		public override void WriteValue(int value)
		{
			base.WriteValue(value);
			method_22(value, JsonToken.Integer);
		}

		[CLSCompliant(false)]
		public override void WriteValue(uint value)
		{
			base.WriteValue(value);
			method_22(value, JsonToken.Integer);
		}

		public override void WriteValue(long value)
		{
			base.WriteValue(value);
			method_22(value, JsonToken.Integer);
		}

		[CLSCompliant(false)]
		public override void WriteValue(ulong value)
		{
			base.WriteValue(value);
			method_22(value, JsonToken.Integer);
		}

		public override void WriteValue(float value)
		{
			base.WriteValue(value);
			method_22(value, JsonToken.Float);
		}

		public override void WriteValue(double value)
		{
			base.WriteValue(value);
			method_22(value, JsonToken.Float);
		}

		public override void WriteValue(bool value)
		{
			base.WriteValue(value);
			method_22(value, JsonToken.Boolean);
		}

		public override void WriteValue(short value)
		{
			base.WriteValue(value);
			method_22(value, JsonToken.Integer);
		}

		[CLSCompliant(false)]
		public override void WriteValue(ushort value)
		{
			base.WriteValue(value);
			method_22(value, JsonToken.Integer);
		}

		public override void WriteValue(char value)
		{
			base.WriteValue(value);
			string text = null;
			text = value.ToString(CultureInfo.InvariantCulture);
			method_22(text, JsonToken.String);
		}

		public override void WriteValue(byte value)
		{
			base.WriteValue(value);
			method_22(value, JsonToken.Integer);
		}

		[CLSCompliant(false)]
		public override void WriteValue(sbyte value)
		{
			base.WriteValue(value);
			method_22(value, JsonToken.Integer);
		}

		public override void WriteValue(decimal value)
		{
			base.WriteValue(value);
			method_22(value, JsonToken.Float);
		}

		public override void WriteValue(DateTime value)
		{
			base.WriteValue(value);
			value = Class185.smethod_2(value, base.DateTimeZoneHandling);
			method_22(value, JsonToken.Date);
		}

		public override void WriteValue(DateTimeOffset value)
		{
			base.WriteValue(value);
			method_22(value, JsonToken.Date);
		}

		public override void WriteValue(byte[] value)
		{
			base.WriteValue(value);
			method_22(value, JsonToken.Bytes);
		}

		public override void WriteValue(TimeSpan value)
		{
			base.WriteValue(value);
			method_22(value, JsonToken.String);
		}

		public override void WriteValue(Guid value)
		{
			base.WriteValue(value);
			method_22(value, JsonToken.String);
		}

		public override void WriteValue(Uri value)
		{
			base.WriteValue(value);
			method_22(value, JsonToken.String);
		}
	}
}
